package com.cheng.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;

import com.cheng.main.Main;


public class DataUtil {
	private static DataUtil dataUtil;
	private String ms;			//姓名/性别/籍贯/民族/出生年月/学校/学号/入学年份/学生证编号/备注
	private String ps;			//隐私问题/答案
	
	private BufferedImage mImage;
	private BufferedImage pImage;
	private BufferedImage sImage;
	private Image icon;
	
	private DataUtil(){
		dataUtil = this;
	}
	
	public static DataUtil instance(){
		if(dataUtil == null)
			dataUtil = new DataUtil();
		return dataUtil;
	}
	
	public void clearAll(){
		dataUtil = null;
		dataUtil = new DataUtil();
	}
	
	public Image getIcon(){
		try {  
            // 该方法会将图像加载到内存，从而拿到图像的详细信息。  
        	icon = ImageIO.read(Main.class.getResourceAsStream("/icon.png"));  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
		return icon;
	}
	
	public String infoShow(){
		if(ms.equals("") || ps.equals("")){
			return "error";
		}else{
			String[] mss = ms.split("/");
			String[] pss = ps.split("/");
			String info = "姓名/性别/籍贯/民族/出生年月/学校/学号/入学年份/学生证编号/备注/隐私问题/答案";
			String[] infos = info.split("/");
			StringBuffer sb = new StringBuffer();
			for(int i = 0;i < infos.length;i++){
				sb.append(infos[i]);
				sb.append(":");
				if(i < mss.length){
					sb.append(mss[i]);
				}else{
					sb.append(pss[i-mss.length]);
				}
				if(i < infos.length-1){
					sb.append("\n");
				}
			}
			return sb.toString();
		}
	}
	
	public void CreateCode() throws IOException{
		String p,m,s;
		p = BASE64.encodeByteToString(RSA.encrypt(getPs()));
		System.out.println("p:" + p);
		m = getMs() + "/" + SHA1.toSHA1(p);
		System.out.println("m:" + m);
		s = BASE64.encodeByteToString(RSA.encrypt(SHA1.toSHA1(m)));
		System.out.println("s:" + s);
		TwoDimensionCode handler = new TwoDimensionCode();
		setpImage(handler.encoderQRCode(p, ""));
		setmImage(handler.encoderQRCode(m, ""));
		setsImage(handler.encoderQRCode(s, ""));
	}
	
	public int KeepCode(String path){
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		File mimgFile = new File(path + "\\数字母章.png");
		File pimgFile = new File(path + "\\数字私章.png");
		File simgFile = new File(path + "\\数字子章.png");
		// 生成二维码QRCode图片
		try {
			ImageIO.write(mImage, "png", mimgFile);
			ImageIO.write(pImage, "png", pimgFile);
			ImageIO.write(sImage, "png", simgFile);
			return 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public String getMs() {
		return ms;
	}

	public void setMs(String ms) {
		this.ms = ms;
	}

	public String getPs() {
		return ps;
	}

	public void setPs(String ps) {
		this.ps = ps;
	}

	public BufferedImage getmImage() {
		return mImage;
	}

	public void setmImage(BufferedImage mImage) {
		this.mImage = mImage;
	}

	public BufferedImage getpImage() {
		return pImage;
	}

	public void setpImage(BufferedImage pImage) {
		this.pImage = pImage;
	}

	public BufferedImage getsImage() {
		return sImage;
	}

	public void setsImage(BufferedImage sImage) {
		this.sImage = sImage;
	}
	
}
